<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once 'config.inc.php';
require_once 'common.inc.php';

require_once 'eleontev/Validator.php';
require_once 'eleontev/URL/RequestDataUtil.php';
require_once 'eleontev/Util/PasswordUtil.php';

require_once 'core/app/AppMailSender.php';
require_once APP_MODULE_DIR . 'user/inc/UserModel.php';


$db =& DBUtil::connect($conf);
$reg =& Registry::instance();
$reg->setEntry('db', $db);
$reg->setEntry('tbl_pref', $conf['tbl_pref']);
$reg->setEntry('conf', $conf);
$reg->setEntry('extra', $conf['extra']);

$msg = '';


if(isset($_POST['submit']) && !APP_DEMO_MODE) {	
	
	$v =& new Validator($_POST, false);
	$v->regex('email_msg', 'email', 'email');
	
	$tbl = (object) AppModel::_setTableNames(array('user'), $conf['tbl_pref']);

	$pass = new PasswordUtil;
	$pass->table = $tbl->user;
	
	$user_id = $pass->isEmailExists($_POST['email']);
	if(!$user_id) { $v->setError('email_not_exists_msg', '1'); }
	
	
	if($v->is_error) {
		RequestDataUtil::stripVars($_POST, array(), true);
		$msg = AppMsg::errorBox($v->getErrors());
		
	} else {
		
		$u = new UserModel();
		$user = $u->getById($user_id);
		$password = $pass->generatePassword();
		
		$mail = &new AppMailSender();
		$sent = $mail->sendGeneratedPassword($password, APP_HOME_PATH, $user);
		
		if($sent) {
			$pass->setPassword($user_id, MD5($password));
			
			header('Location:' . $_SERVER['PHP_SELF'] . '?sent=1');
			exit();
		} else {
			header('Location:' . $_SERVER['PHP_SELF'] . '?sent=2');
			exit();			
		}
	}
}

$file = AppMsg::getCommonMsgFile('common_msg.ini');
$msgs = AppMsg::parseMsgs($file, false, false);

$tpl = new tplTemplatez(APP_TMPL_DIR . 'restore_password.html');
$tpl->tplAssign('meta_charset', $conf['lang']['meta_charset']);


if(@$_GET['sent'] == 1) {
	$msg = AppMsg::afterActionBox('password_sent', 'success');
	$tpl->tplAssign('login_link', APP_HOME_PATH);
	
	
} elseif (@$_GET['sent'] == 2) {
	$msg = AppMsg::afterActionBox('password_not_sent', 'error');
}

if(@$_GET['sent'] == 2 || empty($_GET['sent'])) {
	$tpl->tplSetNeeded('/form'); 
}


if(APP_DEMO_MODE) {
	$msg = AppMsg::afterActionBox('not_allowed_demo', 'error');
}


$tpl->tplAssign($msgs);
$tpl->tplAssign('msg', $msg);
$tpl->tplAssign('login_link', 'login.php');

$tpl->tplParse($_POST);
$tpl->tplPrint();
?>
